﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace L04
{
    public partial class FrmMain : Form
    {
        private int x, y;
        private int timerTickCount = 0;
        private Bitmap bmAll;
        private Bitmap[] bmFragments = new Bitmap[12];
        private int displayIndex = 0;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("窗体大小（像素）：{0}，{1}",
                this.Width.ToString(),
                this.Height.ToString()
            );
            sb.AppendLine();
            sb.AppendFormat(
                "工作区域大小（像素）：{0}，{1}",
                this.ClientRectangle.Width.ToString(),
                this.ClientRectangle.Height.ToString()
            );
            MessageBox.Show(sb.ToString(), "信息提示");
            //this.toolStripStatusLabel1.Text = "计时次数：" + timerTickCount.ToString();
            //this.timer1.Interval = 1000;
            //this.timer1.Start();

            bmAll = new Bitmap(Application.StartupPath + "\\z.jpg");

            for (int row = 0; row < 3; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    bmFragments[row * 1 + col] = bmAll.Clone(
                        new Rectangle(col * 200, row * 113, 200, 113),
                        bmAll.PixelFormat);
                }
            }

            //this.timer2.Interval = 200;
            //this.timer2.Start();

        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine(e.KeyCode.ToString());
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            this.x = e.X;
            this.y = e.Y;
            this.Invalidate();
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("鼠标当前坐标：{0},{1}", x.ToString(), y.ToString());
            e.Graphics.DrawString(sb.ToString(),
                new Font("黑体", 10f),
                new SolidBrush(Color.Pink),
                0, this.ClientRectangle.Height - 38);

            Pen pen = new Pen(Color.Black, 2.0f);
            pen.Color = Color.LightPink;
            g.DrawRectangle(pen, 0, 0, 600, 338);

            g.DrawString("哇咔咔",
                new Font("黑体", 26),
                new SolidBrush(Color.Pink),
                new Point(this.ClientRectangle.Width / 6 - 46, ClientRectangle.Height / 2));

            Bitmap bm = new Bitmap(Application.StartupPath + "\\b.png");
            g.DrawImage(bm, new Rectangle(300, 145, 250, 202));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timerTickCount++;
            //this.toolStripStatusLabel1.Text = "计时次数：" + timerTickCount.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            displayIndex = (++displayIndex) % 12;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap buff = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            Graphics g = Graphics.FromImage(buff);

            g.DrawImage(bmFragments[displayIndex], new Point(10, 10));

            e.Graphics.DrawImage(buff, 0, 0);

            g.Dispose();
            base.OnPaint(e);
        }

    }
}
